using System;
using System.Collections.Generic;
using Csla;
using System.Data;
using ENT.GP.Datos.MysqlHelper;

namespace ENT.GP.Core
{
    [Serializable]
    public class EmpresaLista :
    ReadOnlyListBase<EmpresaLista, EmpresaInfo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(UsuarioLista), "Role");
        }

        #endregion

        #region Factory Methods

        public static EmpresaLista GetEmpresaLista(EmpresaCriterio filter)
        {
            return DataPortal.Fetch<EmpresaLista>(filter);
        }

        private EmpresaLista()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch(EmpresaCriterio criteria)
        {

                       
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[3];

            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("NOMBRE", criteria.Nombre);
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("DESCRIPCION", criteria.Descripcion);
            parametros[2] = new MySql.Data.MySqlClient.MySqlParameter("ACTIVO1", criteria.Estado);
       
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "BuscarEmpresas").Tables[0].Rows)
            {
                this.Add(EmpresaInfo.GetRolInfo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class EmpresaInfo : ReadOnlyBase<EmpresaInfo>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods

        public int Id { get; private set; }
        public string Nombre { get; private set; }
        public string Descripcion { get; private set; }
        public string Codigo { get; private set; }
        public string UrlLogo { get; private set; }
        public int Estado { get; private set; }


        #endregion

        #region Factory Methods

        internal static EmpresaInfo GetRolInfo(object childData)
        {
            return DataPortal.FetchChild<EmpresaInfo>(childData);
        }

        private EmpresaInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            Id = Convert.ToInt32(childData[0].ToString());
            Nombre = childData[1].ToString();
            Descripcion = childData[2].ToString();
            Codigo = childData[3].ToString();
            UrlLogo = childData[4].ToString();
            Estado = Convert.ToInt32(childData[5].ToString());
        }

        #endregion
    }
}
